<template>
  <div class="page-shopping-cart" id="shopping-cart">
    <h4 class="cart-title">购物清单</h4>
    <div class="cart-product-title clearfix">
      <div class="td-check fl">
        <span
          class="check-span fl check-all"
          :class="{ 'checkOk': checkAll }"
          @click="checkBox(checkAll)"
        ></span
        >全选
      </div>
      <div class="td-product fl">商品</div>
      <div class="td-num fl">数量</div>
      <div class="td-price fl">单价(元)</div>
      <div class="td-total fl">金额(元)</div>
      <div class="td-do fl">操作</div>
    </div>
    <div class="cart-product clearfix">
      <!-- 表格 -->
      <table>
        <tbody>
          <tr v-for="(item, index) in productList">
            <td class="td-check">
              <span
                class="check-span"
                :class="{ 'checkOk': item.select }"
                @click="item.select = !item.select"
              ></span>
            </td>
            <td class="td-product">
              <img :src="require(`../assets/${item.pro_img}`)" width="98" height="98" />
              <div class="product-info">
                <h6>{{ item.pro_name }}</h6>
                <p>
                  品牌：{{ item.pro_brand }}&ensp;&ensp;产地：{{
                    item.pro_place
                  }}
                </p>
                <p>
                  规格/纯度:{{ item.pro_purity }}&ensp;&ensp;起定量：{{
                    item.pro_min
                  }}
                </p>
                <p>配送仓储：{{ item.pro_depot }}</p>
              </div>
              <div class="clearfix"></div>
            </td>
            <td class="td-num">
              <div class="product-num">
                <a
                  href="javascript:;"
                  class="num-reduce num-do fl"
                  @click="item.pro_num > 1 ? item.pro_num-- : ''"
                  ><span></span
                ></a>
                <input type="text" class="num-input" v-model="item.pro_num" />
                <a
                  href="javascript:;"
                  class="num-add num-do fr"
                  @click="item.pro_num++"
                  ><span></span
                ></a>
              </div>
            </td>
            <td class="td-price">
              <p class="red-text">
                ￥<span class="price-text">{{
                  item.pro_price.toFixed(2)
                }}</span>
              </p>
            </td>
            <td class="td-total">
              <p class="red-text">
                ￥<span class="total-text">{{
                  item.pro_price * item.pro_num
                }}</span
                >.00
              </p>
            </td>
            <td class="td-do">
              <a href="javascript:;" class="product-delect" @click="del(index)"
                >删除</a
              >
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="cart-product-info">
      <a class="delect-product" href="javascript:;" @click="delCheck"
        ><span></span>删除所选商品</a
      >
      <a class="keep-shopping" href="#"><span></span>继续购物</a>
      <a class="btn-buy fr" href="javascript:;">去结算</a>
      <p class="fr product-total">
        ￥<span>{{ total.price }}</span>
      </p>
      <p class="fr check-num">
        <span>{{ total.numlength }}</span
        >件商品总计（不含运费）：
      </p>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  components: {},
  data() {
    return {
      productList: [
        {
          pro_name: "甘油", //产品名称
          pro_brand: "skc", //品牌名称
          pro_place: "韩国", //产地
          pro_purity: "99.7%", //规格
          pro_min: "215千克", //最小起订量
          pro_depot: "上海仓海仓储", //所在仓库
          pro_num: 1, //数量
          pro_img: "apple.jpg", //图片链接
          pro_price: 800, //单价
        },
        {
          pro_name: "斯文",
          pro_brand: "skc",
          pro_place: "韩国",
          pro_purity: "99.7%",
          pro_min: "215千克",
          pro_depot: "上海仓海仓储",
          pro_num: 2,
          pro_img: "apple.jpg",
          pro_price: 800,
        },
        {
          pro_name: "丙三醇",
          pro_brand: "skc",
          pro_place: "韩国",
          pro_purity: "99.7%",
          pro_min: "215千克",
          pro_depot: "上海仓海仓储",
          pro_num: 3,
          pro_img: "apple.jpg",
          pro_price: 800,
        },
      ],
    };
  },
  created() {},
  methods: {
    //全选
    checkBox(checkAll) {
      for (let i = 0; i < this.productList.length; i++) {
        this.productList[i]["select"] = !checkAll;
      }
    },
    // 删除
    del(index) {
      this.productList.splice(index, 1);
    },
    delCheck() {
      //过滤出没有选中的，选中的在数组中被删除
      this.productList = this.productList.filter( (val)=> {
        return !val.select;
      });
    },
  },
  //计算属性
  computed: {
    //是否全选 全真返回真，否则返回假
    checkAll() {
      return this.productList.every( (val)=> {
        return val.select;
      });
    },
    // 计算总价钱
    total() {
      var arrlist = this.productList.filter( (val)=> {
          return val.select;
        }),
        price = 0;
        // 循环遍历   总价钱
      for (let i = 0; i < arrlist.length; i++) {
        price += arrlist[i].pro_num * arrlist[i].pro_price;
      }
      return { numlength: arrlist.length, price: price };
    },
  },
  mounted () {
    var arr = this;
    this.productList.map(function (val) {
      //购物车的单选按钮，选中商品功能，接口里面没有字段，是监听不到的，通过$set设置一个不存在的变量，
      // 放在data里面 $set(obj, key, val)
      arr.$set(val, "select", true); 
    });
  },
};
</script>
<style scoped lang='scss'>
.fl {
  float: left;
}
.fr {
  float: right;
}
.clearfix {
  zoom: 1;
}

.clearfix:after {
  clear: both;
}

.clearfix:after {
  content: ".";
  display: block;
  overflow: hidden;
  visibility: hidden;
  font-size: 0;
  line-height: 0;
  width: 0;
  height: 0;
}

a {
  text-decoration: none;
  color: #333;
}

img {
  vertical-align: middle;
}

.page-shopping-cart {
  width: 1200px;
  margin: 50px auto;
  font-size: 14px;
  border: 1px solid #e3e3e3;
  border-top: 2px solid #317ee7;
}

.page-shopping-cart .cart-title {
  color: #317ee7;
  font-size: 16px;
  text-align: left;
  padding-left: 20px;
  line-height: 68px;
}

.page-shopping-cart .red-text {
  color: #e94826;
}

.page-shopping-cart .check-span {
  display: block;
  width: 24px;
  height: 20px;
  background: url("../assets/jlt.png") no-repeat 0 0;
}

.page-shopping-cart .check-span.checkOk {
  background: url("../assets/jlt.png") no-repeat 0 -22px;
}

.page-shopping-cart .td-check {
  width: 70px;
}

.page-shopping-cart .td-product {
  width: 460px;
}

.page-shopping-cart .td-num,
.page-shopping-cart .td-price,
.page-shopping-cart .td-total {
  width: 160px;
}

.page-shopping-cart .td-do {
  width: 150px;
}

.page-shopping-cart .cart-product-title {
  text-align: center;
  height: 38px;
  line-height: 38px;
  padding: 0 20px;
  background: #f7f7f7;
  border-top: 1px solid #e3e3e3;
  border-bottom: 1px solid #e3e3e3;
}

.page-shopping-cart .cart-product-title .td-product {
  text-align: center;
  font-size: 14px;
}

.page-shopping-cart .cart-product-title .td-check {
  text-align: left;
}

.page-shopping-cart .cart-product-title .td-check .check-span {
  margin: 9px 6px 0 0;
}

.page-shopping-cart .cart-product {
  padding: 0 20px;
  text-align: center;
}

.page-shopping-cart .cart-product table {
  width: 100%;
  text-align: center;
  font-size: 14px;
}

.page-shopping-cart .cart-product table td {
  padding: 20px 0;
}

.page-shopping-cart .cart-product table tr {
  border-bottom: 1px dashed #e3e3e3;
}

.page-shopping-cart .cart-product table tr:last-child {
  border-bottom: none;
}

.page-shopping-cart .cart-product table .product-num {
  border: 1px solid #e3e3e3;
  display: inline-block;
  text-align: center;
}

.page-shopping-cart .cart-product table .product-num .num-do {
  width: 24px;
  height: 28px;
  display: block;
  background: #f7f7f7;
}

.page-shopping-cart .cart-product table .product-num .num-reduce span {
  background: url("../assets/jlt.png") no-repeat -40px -22px;
  display: block;
  width: 6px;
  height: 2px;
  margin: 13px auto 0 auto;
}

.page-shopping-cart .cart-product table .product-num .num-add span {
  background: url("../assets/jlt.png") no-repeat -60px -22px;
  display: block;
  width: 8px;
  height: 8px;
  margin: 10px auto 0 auto;
}

.page-shopping-cart .cart-product table .product-num .num-input {
  width: 42px;
  height: 28px;
  line-height: 28px;
  border: none;
  text-align: center;
}

.page-shopping-cart .cart-product table .td-product {
  text-align: left;
  font-size: 12px;
  line-height: 20px;
}

.page-shopping-cart .cart-product table .td-product img {
  border: 1px solid #e3e3e3;
  margin-right: 10px;
}

.page-shopping-cart .cart-product table .td-product .product-info {
  display: inline-block;
  vertical-align: middle;
}

.page-shopping-cart .cart-product table .td-do {
  font-size: 12px;
}

.page-shopping-cart .cart-product-info {
  height: 50px;
  line-height: 50px;
  background: #f7f7f7;
  padding-left: 20px;
}

.page-shopping-cart .cart-product-info .delect-product {
  color: #666;
}

.page-shopping-cart .cart-product-info .delect-product span {
  display: inline-block;
  vertical-align: top;
  margin: 18px 8px 0 0;
  width: 13px;
  height: 15px;
  background: url("../assets/jlt.png") no-repeat -60px 0;
}

.page-shopping-cart .cart-product-info .product-total {
  font-size: 14px;
  color: #e94826;
}

.page-shopping-cart .cart-product-info .product-total span {
  font-size: 20px;
}

.page-shopping-cart .cart-product-info .check-num {
  color: #333;
}

.page-shopping-cart .cart-product-info .check-num span {
  color: #e94826;
}

.page-shopping-cart .cart-product-info .keep-shopping {
  color: #666;
  margin-left: 40px;
}

.page-shopping-cart .cart-product-info .keep-shopping span {
  display: inline-block;
  vertical-align: top;
  margin: 18px 8px 0 0;
  width: 15px;
  height: 15px;
  background: url("../assets/jlt.png") no-repeat -40px 0;
}

.page-shopping-cart .cart-product-info .btn-buy {
  height: 50px;
  color: #fff;
  font-size: 20px;
  display: block;
  width: 110px;
  background: #ff7700;
  text-align: center;
  margin-left: 30px;
}

.page-shopping-cart .cart-worder {
  padding: 20px;
}

.page-shopping-cart .cart-worder .choose-worder {
  color: #fff;
  display: block;
  background: #39e;
  width: 140px;
  height: 40px;
  line-height: 40px;
  border-radius: 4px;
  text-align: center;
  margin-right: 20px;
}

.page-shopping-cart .cart-worder .choose-worder span {
  display: inline-block;
  vertical-align: top;
  margin: 9px 10px 0 0;
  width: 22px;
  height: 22px;
  background: url("../assets/jlt.png") no-repeat -92px 0;
}

.page-shopping-cart .cart-worder .worker-info {
  color: #666;
}

.page-shopping-cart .cart-worder .worker-info img {
  border-radius: 100%;
  margin-right: 10px;
}

.page-shopping-cart .cart-worder .worker-info span {
  color: #000;
}

.choose-worker-box {
  width: 620px;
  background: #fff;
}

.choose-worker-box .box-title {
  height: 40px;
  line-height: 40px;
  background: #f7f7f7;
  text-align: center;
  position: relative;
  font-size: 14px;
}

.choose-worker-box .box-title a {
  display: block;
  position: absolute;
  top: 15px;
  right: 16px;
  width: 10px;
  height: 10px;
  background: url("../assets/jlt.png") no-repeat -80px 0;
}

.choose-worker-box .box-title a:hover {
  background: url("../assets/jlt.png") no-repeat -80px -22px;
}

.choose-worker-box .worker-list {
  padding-top: 30px;
  height: 134px;
  overflow-y: auto;
}

.choose-worker-box .worker-list li {
  float: left;
  width: 25%;
  text-align: center;
  margin-bottom: 30px;
}

.choose-worker-box .worker-list li p {
  margin-top: 8px;
}

.choose-worker-box .worker-list li.cur a {
  color: #f70;
}

.choose-worker-box .worker-list li.cur a img {
  border: 1px solid #f70;
}

.choose-worker-box .worker-list li a:hover {
  color: #f70;
}

.choose-worker-box .worker-list li a:hover img {
  border: 1px solid #f70;
}

.choose-worker-box .worker-list li img {
  border: 1px solid #fff;
  border-radius: 100%;
}
// .shopCart {
//   width: 1200px;
//   margin: 50px auto;
//   border: 1px solid #ccc;
//   border-top: 2px solid #317ee7;
//   h4 {
//     color: #317ee7;
//     padding-left: 20px;
//     line-height: 60px;
//   }
//   .cartTitle {
//     height: 40px;
//     line-height: 40px;
//     text-align: center;
//     padding: 0 20px;
//     background-color: #f7f7f7;
//     border-top: 1px solid #e3e3e3;
//     border-bottom: 1px solid #e3e3e3;
//     padding-bottom: 50px;
//   }
//   .show {
//     text-align: left;
//     margin-top: -41px;
//     margin-left: 250px;
//   }
//   .check {
//     text-align: left;
//     width: 70px;
//   }
//   .num {
//     text-align: left;
//     margin-top: -41px;
//     margin-left: 502px;
//   }
//   .price {
//     margin-top: -41px;
//     margin-left: 310px;
//   }
//   .total {
//     margin-top: -41px;
//     margin-left: 680px;
//   }
//   .do {
//     margin-top: -41px;
//     margin-left: 950px;
//   }
//   .spanCheck{
//     display: block;
//     width: 24px;
//     height: 20px;
//   }
//   .check,
//   .spanCheck {
//     margin: 9px 6px 0 0;
//   }
// }
</style>
